<?php
	error_reporting(E_ALL^E_NOTICE);
	require_once ('../../../config/config.php'); 
	require_once ('../../../class/email.class.php'); 
	require_once ('../../../class/tools.class.php');
	date_default_timezone_set("PRC");
	session_start();
	if(empty($_SESSION['admin'])){
		echo "<script>location='../no.php';</script>";
		exit();
	}else{
    if($_GET['problem_book_id']){
      $getbooksRs=mysqli_fetch_array(mysqli_query($con,"select * from problem_book where problem_book_id='".$_GET['problem_book_id']."'"));
			if($_GET['oper']=="yes"){
        mysqli_query($con,"UPDATE `problem_book` SET `state` = '1' WHERE `problem_book_id` = '".$_GET['problem_book_id']."'");
				mysqli_query($con,"UPDATE `borrowing_record` SET `state` = '7' WHERE `brid` = '".$getbooksRs['brid']."'");
        $bnoRs=mysqli_fetch_array(mysqli_query($con,"select * from order_record where `rid`='".$getbooksRs['oid']."'"));
				mysqli_query($con,"INSERT INTO `refund_record` (`refund_id`, `order_no`, `time`) VALUES (NULL, '".$bnoRs['order_no']."', '".date('Y/m/d H:i:s')."')");
				echo "<script>alert('退借成功，确认后为您退还押金！');location='../../../pay/refund.php?order_no=".$bnoRs['order_no']."';</script>";
				exit();
        exit();
      }
      if($_GET['oper']=="no"){
        mysqli_query($con,"UPDATE `borrowing_record` SET `state` = '6' WHERE `brid` = '".$getbooksRs['brid']."'");
        mysqli_query($con,"UPDATE `problem_book` SET `state` = '2' WHERE `problem_book_id` = '".$_GET['problem_book_id']."'");
        $bookInfo=mysqli_fetch_array(mysqli_query($con,"select * from book_sharing where bid='".$getbooksRs['bid']."'"));
        $userInfo=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM `user` where `uid`='".$getbooksRs['touid']."'"));
        $money=$userInfo['money']+$bookInfo['book_price'];
        mysqli_query($con,"UPDATE `user` SET `money` = '".$money."' WHERE `user`.`uid` = '".$getbooksRs['touid']."'");
        echo "<script>alert('以押抵书！');location='./problem_book.php';</script>";
        exit();
      }
    }
  }
?>
<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script src="../../js/jquery.min.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:38px">ဂ</i></a>
    </div>
    <div class="x-body">
      <table class="layui-table">
        <thead>
          <tr>
            <th>Problem_Book_ID</th>
            <th>到期时间</th>
            <th>关联图书</th>
            <th>订单号</th>
            <th>借阅用户</th>
            <th>图书所有者</th>
            <th>异常描述</th>
            <th>处理状态</th>
            <th>操作</th>
        </thead>
        <tbody>
          <?php
            $books=mysqli_query($con,"select * from problem_book order by problem_book_id asc");
            while($booksRs=mysqli_fetch_array($books)){ 
              ?>
              <tr>
                <td><?php echo $booksRs['problem_book_id'];?></td>
                <td>
                  <?php 
                    $brRs=mysqli_fetch_array(mysqli_query($con,"select * from borrowing_record where brid='".$booksRs['brid']."'"));
                    if($brRs['renew']=="1"){
                      $days=$brRs['days']+$brRs['new_over_time'];
                      echo date("Y/m/d", strtotime("+".$days." days", strtotime($brRs['inspect_time'])));
                    }else{
                      echo date("Y/m/d", strtotime("+".$brRs['days']." days", strtotime($brRs['inspect_time'])));
                    }
                  ?>
                </td>
                <td>
                  <?php 
                    $bookRs=mysqli_fetch_array(mysqli_query($con,"select * from book_sharing where bid='".$booksRs['bid']."'"));
                    echo $bookRs['book_name'];
                  ?>
                </td>
                <td>
                  <?php 
                    $order_record=mysqli_fetch_array(mysqli_query($con,"select * from order_record where rid='".$booksRs['oid']."'"));
                    echo $order_record['order_no'];
                  ?>
                </td>
                <td>
                  <?php 
                    $userRs=mysqli_fetch_array(mysqli_query($con,"select * from user where uid='".$booksRs['uid']."'"));
                    echo $userRs['user'];
                  ?>
                </td>
                <td>
                  <?php 
                    $userRs2=mysqli_fetch_array(mysqli_query($con,"select * from user where uid='".$booksRs['touid']."'"));
                    echo $userRs2['user'];
                  ?>
                </td>
                <td><a href="JavaScript:alert('<?php echo $booksRs['more'];?>');">查看</a></td>
                <td>
                  <?php 
                    if($booksRs['state']=="0"){
                      echo "未处理";
                    }elseif($booksRs['state']=="1"){
                      echo "退押留书";
                    }if($booksRs['state']=="2"){
                      echo "以押抵书";
                    }
                  ?>
                </td>
                <td class="td-manage">
                  <?php 
                    if($booksRs['state']=="0"){
                      echo "<a href='?oper=yes&problem_book_id=".$booksRs['problem_book_id']."'><span class='layui-btn layui-btn-normal layui-btn-mini'>退押留书</span></a>";
                      echo "&nbsp;<a href='?oper=no&problem_book_id=".$booksRs['problem_book_id']."'><span class='layui-btn layui-btn-normal layui-btn-mini'>以押低书</span></a>";
                    }else{
                      echo "无法操作";
                    }
                  ?>
                </td>
              </tr>
          <?php    
            }
            echo "<tr><td colspan='9'>暂无更多...</td></tr>";
          ?>
        </tbody>
      </table>
    </div>
  </body>
</html>